package com.gps.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ReadWrite {

    public static byte[] readBytes(InputStream is, boolean fully) throws IOException {
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // 读取输入流的所有内容，知道读完为止
        while (true) {
            int length = is.read(buffer);
            if(-1 == length)                    // 说明读完了
                break;
            baos.write(buffer, 0, length);  // 根据实际长度，将buffer写入字节数组输出流
            if (!fully && length != bufferSize)           // 也说明读完了
                break;
        }
        byte[] result = baos.toByteArray();     // 将字节数组输出流的内容导出为字节数组
        return result;
    }
}
